package main

import (
	"crypto/sha1"
	"github.com/xtaci/kcp-go/v5"
	"golang.org/x/crypto/pbkdf2"
	"io"
	"log"
	"time"
)

func main() {
	key := pbkdf2.Key([]byte("123456"), []byte("123"), 1024, 32, sha1.New)
	block, _ := kcp.NewAESBlockCrypt(key)
	if sess, err := kcp.DialWithOptions("127.0.0.1:9001", block, 10, 3); err == nil {
		go func() {
			for {
				data := time.Now().String()
				_, err := sess.Write([]byte(data))
				if err != nil {
					log.Fatal(err)
				}
				time.Sleep(time.Second * 10)
			}
		}()
		for {
			buf := make([]byte, 1)
			if _, err := io.ReadFull(sess, buf); err == nil {
				log.Println(string(buf))
			} else {
				log.Fatal(err)
			}
		}
	} else {
		log.Fatal(err)
	}
}
